package com.moxiao.yfjh.module.order.tradeRefund.brokeragerecord;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.order.controller.admin.brokeragerecord.vo.*;
import com.moxiao.yfjh.module.order.controller.app.vo.AppWithdrawalReqVo;
import com.moxiao.yfjh.module.order.convert.brokeragerecord.BlindBrokerageRecordConvert;
import com.moxiao.yfjh.module.order.dal.dataobject.brokeragerecord.BlindBrokerageRecordDO;
import com.moxiao.yfjh.module.order.dal.mysql.brokeragerecord.BlindBrokerageRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.order.enums.ErrorCodeConstants.BROKERAGE_RECORD_NOT_EXISTS;

/**
 * 相亲佣金记录 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class BlindBrokerageRecordServiceImpl implements BlindBrokerageRecordService {

    @Resource
    private BlindBrokerageRecordMapper blindBrokerageRecordMapper;

    @Override
    public Integer createBrokerageRecord(BlindBrokerageRecordCreateReqVO createReqVO) {
        // 插入
        BlindBrokerageRecordDO brokerageRecord = BlindBrokerageRecordConvert.INSTANCE.convert(createReqVO);
        blindBrokerageRecordMapper.insert(brokerageRecord);
        // 返回
        return brokerageRecord.getId();
    }

    @Override
    public void updateBrokerageRecord(BlindBrokerageRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateBrokerageRecordExists(updateReqVO.getId());
        // 更新
        BlindBrokerageRecordDO updateObj = BlindBrokerageRecordConvert.INSTANCE.convert(updateReqVO);
        blindBrokerageRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteBrokerageRecord(Integer id) {
        // 校验存在
        validateBrokerageRecordExists(id);
        // 删除
        blindBrokerageRecordMapper.deleteById(id);
    }

    private void validateBrokerageRecordExists(Integer id) {
        if (blindBrokerageRecordMapper.selectById(id) == null) {
            throw exception(BROKERAGE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public BlindBrokerageRecordDO getBrokerageRecord(Integer id) {
        return blindBrokerageRecordMapper.selectById(id);
    }

    @Override
    public List<BlindBrokerageRecordDO> getBrokerageRecordList(Collection<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return blindBrokerageRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlindBrokerageRecordDO> getBrokerageRecordPage(BlindBrokerageRecordPageReqVO pageReqVO) {
        return blindBrokerageRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<BlindBrokerageRecordRespVO> getJoinRecordPage(BlindBrokerageRecordPageReqVO pageReqVO) {
        return blindBrokerageRecordMapper.selectJoinPage(pageReqVO);
    }


    @Override
    public List<BlindBrokerageRecordDO> getBrokerageRecordList(BlindBrokerageRecordExportReqVO exportReqVO) {
        return blindBrokerageRecordMapper.selectList(exportReqVO);
    }

    @Override
    public AppWithdrawalReqVo getWithdrawal(Long userId) {
        AppWithdrawalReqVo vo = new AppWithdrawalReqVo();
        // 总佣金
        BigDecimal withdrawable = blindBrokerageRecordMapper.getWithdrawable(userId);
        vo.setTotalCommission(withdrawable);
        // 已提现金额
        BigDecimal withdrawn = blindBrokerageRecordMapper.getWithdrawn(userId);
        vo.setWithdrawn(withdrawn);
        //提现中
        BigDecimal withdrawalProgress = blindBrokerageRecordMapper.getWithdrawalProgress(userId);
        vo.setWithdrawalProgress(withdrawalProgress);
        // 可提现金额 总佣金 - (提现中+已提现金)
        vo.setWithdrawable(withdrawable.subtract(withdrawn.add(withdrawalProgress)));
        // 团队人数
        Integer team = blindBrokerageRecordMapper.getTeam(String.valueOf(userId));
        vo.setTeamNum(team);
        return vo;
    }


    @Override
    public BigDecimal getBalance(Long userId) {
        return blindBrokerageRecordMapper.getWithdrawable(userId);
    }

    /**
     * 已提现金额
     */
    @Override
    public BigDecimal getWithdrawn(Long userId) {
        // 已提现金额
        return blindBrokerageRecordMapper.getWithdrawn(userId);
    }

    /**
     * 提现中金额
     */
    @Override
    public BigDecimal getWithdrawalProgress(Long userId) {
        return blindBrokerageRecordMapper.getWithdrawalProgress(userId);
    }
}
